package com.offcn.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import lombok.ToString;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Data
@ToString
public class OssTemplate {
    private String endpoint;
    private  String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    //文件上传
    public  String upload(InputStream inputStream,String fileName){
        //1.文件上传
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String folderName = simpleDateFormat.format(new Date());//文件夹名字
        fileName =  UUID.randomUUID().toString().replace("-","")+""+fileName;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, "pic/"+folderName+"/"+fileName, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        String url="http://"+bucketName+endpoint+"/pic/"+folderName+"/"+fileName;
        System.out.println("上传成功");
        return url;

    }
}
